package com.ssm.zyn.action;

import com.ssm.zyn.eo.TeacherEOpage;
import com.ssm.zyn.model.Role;
import com.ssm.zyn.model.Teacher;
import com.ssm.zyn.service.Teacherservice;
import com.ssm.zyn.tool.ResponseMessage;
import com.ssm.zyn.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags ="teacher" )
@RestController
@CrossOrigin
public class TeacherController {
    @Autowired
    private Teacherservice teacherservice;
    @ApiOperation(value = "教师添加方法")
    @RequestMapping(value="/addTeacher",method = RequestMethod.GET)
    public ResponseMessage addTeacher(Teacher teacher){
        int IsAdd=0;
        //传入数据的判断
        //（1）实体对象不能为空
        if(teacher!=null){
            //(2)角色名称不能为空
            if(teacher.getTeacherid()!=null){
                //(3)添加角色数据
                IsAdd=teacherservice.AddtTeacher(teacher);
                if(IsAdd==1){
                    return Result.success("教师信息保存成功");
                }else if(IsAdd==-1){
                    return Result.error("教师编号重复不允许添加");
                }else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }
    @ApiOperation(value = "教师信息修改方法")
    @RequestMapping(value="/updateTeacher",method = RequestMethod.GET)
    public ResponseMessage updateTeacher(Teacher teacher){
        int isUpdate=0;
        //传入的参数不能为空
        if(teacher!=null){
            //参数当中主键不能为0

            if (teacher.getTid()!=0){
                isUpdate=teacherservice.UpdateTeacher(teacher);
                if(isUpdate==1){

                    return Result.success("更新成功");
                }else {
                    return Result.error("更新失败");
                }
            }
        }
        return  Result.error("传入参数不正确");
    }
    @ApiOperation(value = "教师删除方法")
    @RequestMapping(value="/delTeacher",method = RequestMethod.GET)
    public ResponseMessage deTeacher(int tid){
        if (tid!=0){
            int delInt=0;
            delInt=teacherservice.DeleteTeacher(tid);
            if(delInt>0){
                return Result.success("删除成功");
            }else {
                return Result.error("与中间表有关联，删除失败");
            }
        }else {
            return Result.error("删除的主键不能为0,请重新输入");
        }
    }
    @ApiOperation(value = "根据条件获取教师信息")
    @RequestMapping(value = "/getTeacherAll",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public ResponseMessage getTeacherAll(@RequestBody TeacherEOpage teacherEOpage){
        Map<String, List> map=new HashMap<>();
        List<TeacherEOpage> listT=teacherservice.getWhereAll(teacherEOpage);
        map.put("list",listT);
        return Result.success("200",map);
    }
}
